package com.dsteiner.xelavos.client.controller;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;

import com.dsteiner.widgets.client.IJsUtil;
import com.dsteiner.widgets.client.Widgets.IFlowPanel;
import com.dsteiner.widgets.client.Widgets.ILazyWidget;
import com.dsteiner.widgets.client.Widgets.IScrollPanel;
import com.dsteiner.widgets.client.Widgets.IWidget;
import com.dsteiner.widgets.client.Widgets.IWidgetBuilder;
import com.dsteiner.widgets.client.Widgets.WidgetListenerAdapter;
import com.dsteiner.widgets.client.domain.ModelObject;
import com.dsteiner.xelavos.client.constants.Constants;
import com.dsteiner.xelavos.client.model.AuthenticationModel;
import com.dsteiner.xelavos.client.model.DisplayModel;
import com.dsteiner.xelavos.client.model.MainTabModel;
import com.dsteiner.xelavos.client.model.TopNewModel;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;

public class TopNewController {

	private IFlowPanel newContentPanel;
	private IScrollPanel recentScrollPanel;
	private IScrollPanel activeScrollPanel;
	private IFlowPanel topContentPanel;
	private TopNewModel topNewModel;
	private IWidgetBuilder widgetBuilder;
	private IJsUtil jsUtil;
	private MainTabModel mainTabModel;
	private AuthenticationModel authenticationModel;

	public void registerActiveContentPanel(IFlowPanel activeContentPanel) {
		this.topContentPanel = activeContentPanel;
	}
	
	public void registerActiveScrollPanel(IScrollPanel activeScrollPanel) {
		this.activeScrollPanel = activeScrollPanel;
	}

	public void registerJsUtil(IJsUtil jsUtil) {
		this.jsUtil = jsUtil;
		jsUtil.addWindowResizeHandler(new ResizeHandler(){
			public void onResize(ResizeEvent event) {
				updateScrollPanelHeights();
			}
		});
	}

	public void registerMainTabModel(MainTabModel mainTabModel) {
		this.mainTabModel = mainTabModel;
		mainTabModel.addPropertyChangeListener("selectedTab", new PropertyChangeListener(){
			public void propertyChange(PropertyChangeEvent evt) {
				updateTopNewItems();
			}
		});
	}

	public void registerRecentContentPanel(IFlowPanel recentContentPanel) {
		this.newContentPanel = recentContentPanel;
	}

	public void registerRecentScrollPanel(IScrollPanel recentScrollPanel) {
		this.recentScrollPanel = recentScrollPanel;
	}

	public void registerTopNewModel(TopNewModel topNewModel) {
		this.topNewModel = topNewModel;
		topNewModel.addPropertyChangeListener("itemsTop", new PropertyChangeListener(){
			public void propertyChange(PropertyChangeEvent evt) {
				updateTopWidgets();
			}
		});
		topNewModel.addPropertyChangeListener("itemsNew", new PropertyChangeListener(){
			public void propertyChange(PropertyChangeEvent evt) {
				updateNewWidgets();
			}
		});
		topNewModel.addPropertyChangeListener("itemsTopMap", new PropertyChangeListener(){
			public void propertyChange(PropertyChangeEvent evt) {
				updateTopNewItems();
			}
		});
		topNewModel.addPropertyChangeListener("itemsNewMap", new PropertyChangeListener(){
			public void propertyChange(PropertyChangeEvent evt) {
				updateTopNewItems();
			}
		});
	}

	public void registerUsersWidget(ILazyWidget usersWidget) {
		usersWidget.addWidgetListener(new WidgetListenerAdapter(){
			@Override
			public void onAttach(IWidget widget) {
				updateScrollPanelHeights();
			}
		});
	}
	
	public void registerWidgetBuilder(IWidgetBuilder widgetBuilder) {
		this.widgetBuilder = widgetBuilder;
	}

	private void updateNewWidgets() {
		List<ModelObject> itemsNew = topNewModel.getItemsNew();
		newContentPanel.clear();
		
		if(itemsNew == null)
			return;
		
		for (ModelObject user : itemsNew) {
			DisplayModel displayModel = new DisplayModel();
			displayModel.setModelObject(user);
			IWidget widget = widgetBuilder.createWidget(displayModel);
			newContentPanel.add(widget);
		}
	}

	private void updateScrollPanelHeights() {
		int clientHeight = jsUtil.getClientHeight();
		int scrollPanelHeight = (clientHeight - Constants.Util.getHeaderHeight(authenticationModel.getUser()) - 120) /2;
		recentScrollPanel.setHeight(scrollPanelHeight+"px");
		activeScrollPanel.setHeight(scrollPanelHeight+"px");
	}

	private void updateTopNewItems() {
		String selectedTabClassName = mainTabModel.getSelectedTab().getClassName();
		topNewModel.setItemsNew(topNewModel.getItemsNew(selectedTabClassName));
		topNewModel.setItemsTop(topNewModel.getItemsTop(selectedTabClassName));
	}
	private void updateTopWidgets() {
		List<ModelObject> itemsNew = topNewModel.getItemsTop();
		topContentPanel.clear();
		
		if(itemsNew == null)
			return;
		
		for (ModelObject user : itemsNew) {
			DisplayModel displayModel = new DisplayModel();
			displayModel.setModelObject(user);
			IWidget widget = widgetBuilder.createWidget(displayModel);
			topContentPanel.add(widget);
		}
	}

	public void registerAuthenticationModel(AuthenticationModel authenticationModel) {
		this.authenticationModel = authenticationModel;
		authenticationModel.addPropertyChangeListener("user", new PropertyChangeListener(){
			public void propertyChange(PropertyChangeEvent evt) {
				updateScrollPanelHeights();
			}
		});
	}

}
